<template>
  <div class="app-container">
    <span style="font-size: 14px;font-weight: 700;color: red;font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif;margin-left: 8px"> 请选择配件 : </span>
    <el-select v-model="selectValue" filterable clearable placeholder="请选择" size="mini" @change="selectChange">
      <el-option
        v-for="item in options"
        :key="item.ID"
        :label="item.No"
        :value="item"
        :disabled="item.Disabled"/>
    </el-select>
    <br>
    <br>
    <el-table :data="components" style="width: 100%">
      <el-table-column label="编号" prop="No"/>
      <el-table-column label="名称" prop="Name"/>
      <el-table-column label="安全存量" prop="MerchandiseSafetyStock"/>
      <el-table-column label="库存" prop="StockNumber"/>
      <el-table-column label="单个进价(元)" prop="PurchasePrice"/>
      <el-table-column label="供应商" prop="Supply"/>
      <el-table-column label="单位" prop="Unit">
        <template slot-scope="scope">
          <el-select v-model="scope.row.Unit" filterable clearable placeholder="请选择" size="mini">
            <el-option
              v-for="item in units"
              :key="item.ID"
              :label="item.Name"
              :value="item.ID"/>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="数量" width="150">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.Quantity" :min="0" size="mini" @change="handleChange()"/>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" icon="el-icon-delete" circle @click="handleDelete(scope.$index,scope.row)"/>
        </template>
      </el-table-column>
    </el-table>
    <br>
    <br>
    <el-form v-enterToNext="true" ref="postForm" :model="postForm" :rules="rules" size="mini" status-icon >
      <el-form-item v-if="1===2" label="订单总额" prop="Amount">
        <el-input v-model.number="postForm.Amount" type="number"/>
      </el-form-item>
      <el-form-item label="备注" prop="Remarks">
        <el-input v-model="postForm.Remarks" autosize type="textarea" placeholder="请说明..."/>
      </el-form-item>

      <span style="font-size: 14px;font-weight: 700;color: red;font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif;margin-left: 8px">
        PS:提交后，请联系管理员在【所有配件进货】页面审核。
      </span>
    </el-form>
    <br >
    <div>
      <el-button :loading="loading" type="primary" size="mini" @click="submitForm">提交</el-button>
      <el-button size="mini" @click="resetForm">重置</el-button>
    </div>
  </div>
</template>

<script>

import { getComponents } from '@/api/component'
import { createComponentOrder } from '@/api/order'
export default {
  data() {
    return {
      loading: false,
      selectValue: '',
      options: [],
      components: [],
      units: [
        { ID: '个', Name: '个' },
        { ID: '箱', Name: '箱' }
      ],
      postForm: {
        Amount: null,
        Remarks: '',
        Goods: []
      },
      rules: {
        Remarks: [
          { max: 255, message: '最多255  个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    func() {
      console.log('on enter')
      const DOM = event.target
      const nextDOM = DOM.nextElementSibling
      nextDOM.focus()
    },
    fetchData() {
      getComponents().then(response => {
        console.log(response)
        this.options = response.map(item => {
          return {
            Quantity: 0,
            Unit: '个',
            ID: item.ID,
            No: item.No,
            Type: item.Type,
            Name: item.Name,
            MerchandiseSafetyStock: item.MerchandiseSafetyStock,
            StockNumber: item.StockNumber,
            PurchasePrice: item.PurchasePrice,
            Supply: item.Supply
          }
        })
      })
    },
    submitForm() {
      this.$refs['postForm'].validate(valid => {
        if (valid) {
          const len = this.components.length
          if (len <= 0) {
            this.$message.error('请添加配件')
            return
          }
          for (let i = 0; i < len; i++) {
            if (this.components[i].Quantity <= 0) {
              this.$message.error('配件数量不能为 0')
              this.postForm.Goods = []
              return
            }
            this.postForm.Goods.push({
              ID: this.components[i].ID,
              Number: this.components[i].Quantity,
              Unit: this.components[i].Unit
            })
          }

          console.log(this.postForm)
          this.loading = true
          createComponentOrder(this.postForm)
            .then(response => {
              console.log(response)
              this.loading = false
              this.resetForm()
              this.$message.success('创建成功')
            })
            .catch(() => {
              this.loading = false
              this.postForm.Goods = []
              this.$message.error('创建失败')
            })
        }
      })
    },
    selectChange() {
      if (this.selectValue !== '') {
        this.selectValue.Disabled = true
        this.components.push(this.selectValue)
        this.selectValue = ''
      }
    },
    handleDelete(index, row) {
      this.components.splice(index, 1)
      row.Disabled = false
    },
    handleChange() {

    },
    resetForm() {
      this.postForm.Amount = null
      this.postForm.Remarks = ''
      this.postForm.Goods = []
      for (let i = 0; i < this.components.length; i++) {
        this.components[i].Disabled = false
      }

      this.components = []
    }
  }
}
</script>
